package cz.khfotbal.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.khfotbal.dao.UserDao;
import cz.khfotbal.domain.User;

@Repository
@Transactional
public class UserDaoImpl implements UserDao {

	@Autowired
	private SessionFactory sessionFactory;

	private Session session;

	public List<User> getAllUsers() {
		session = sessionFactory.getCurrentSession();
		List<User> list = session.createQuery("from User").list();
		return list;
	}

	public void updateUser(User user) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(user);

	}

	public void createUser(User user) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(user);

	}

	public void deleteUser(User user) {
		session = sessionFactory.getCurrentSession();
		session.delete(user);
	}

	public User findUser(String username) {
		session = sessionFactory.getCurrentSession();
		User result = (User) session.createQuery(
				"from User u where u.username='" + username + "'")
				.uniqueResult();
		return result;

	}
}
